#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>
#include <workflow/HttpUtil.h>
#include <workflow/Workflow.h>
#include <signal.h>
#include <unistd.h>
#include <iostream>
#include <string>
using std::cout;
using std::cerr;
using std::string;
struct SeriesContext{ //自定义类型
	int number;
	string name;
};
static WFFacilities::WaitGroup waitGroup(1);
void sighandler(int signum){
    waitGroup.done();
}
void redisCallback1(WFRedisTask *redisTask){
	cout << "begin redis callback 1\n";
	SeriesContext * context =  static_cast<SeriesContext *>(series_of(redisTask)->get_context());
	context->number = 24;
	context->name = "Kobe";
	cout << context->number << " " << context->name << " get ball, and pass\n"; 
	cout << "end redis callback 1\n";
}
void redisCallback2(WFRedisTask *redisTask){
	cout << "begin redis callback 2\n";
	SeriesContext * context =  static_cast<SeriesContext *>(series_of(redisTask)->get_context());
	cout << "ball from " << context->number << " " << context->name << "\n"; 
	context->number = 23;
	context->name = "James";
	cout << context->number << " " << context->name << " get ball, and pass\n"; 
	cout << "end redis callback 2\n";
}
void redisCallback3(WFRedisTask *redisTask){
	cout << "begin redis callback 3\n";
	SeriesContext * context =  static_cast<SeriesContext *>(series_of(redisTask)->get_context());
	cout << "ball from " << context->number << " " << context->name << "\n"; 
	context->number = 30;
	context->name = "Curry";
	cout << context->number << " " << context->name << " shoot\n"; 
	cout << "end redis callback 3\n";
}
void seriesCallback(const SeriesWork * series){
	SeriesContext * context =  static_cast<SeriesContext *>(series->get_context());
	delete context;
}
int main(){
    signal(SIGINT,sighandler);
    // 创建3个任务
    WFRedisTask * redisTask1 = WFTaskFactory::create_redis_task("redis://127.0.0.1:6379",10,redisCallback1);
    redisTask1->get_req()->set_request("SET",{"61test","hello"});
	WFRedisTask * redisTask2 = WFTaskFactory::create_redis_task("redis://127.0.0.1:6379",10,redisCallback2);
    redisTask2->get_req()->set_request("SET",{"61test","hello"});
	WFRedisTask * redisTask3 = WFTaskFactory::create_redis_task("redis://127.0.0.1:6379",10,redisCallback3);
    redisTask3->get_req()->set_request("SET",{"61test","hello"});

	// 创建一个序列，将任务加入到序列中
	SeriesWork * series = Workflow::create_series_work(redisTask1,nullptr);
	series->push_back(redisTask2);
	series->push_back(redisTask3);

	// 申请context的内存空间
	SeriesContext *context = new SeriesContext;
	// 把context设置为序列的属性
	series->set_context(context);
	//series->set_callback(seriesCallback);
	series->set_callback([context](const SeriesWork * series){
		delete context;
	});

	// 将序列交给框架
	series->start();
    waitGroup.wait();
    return 0;
}